home *** CD-ROM | disk | FTP | other *** search
- /****************************************************/
- /* */
- /* Modeless Code */
- /* */
- /* Copyright 1993, Dave Mark, All rights reserved */
- /* This code is proprietary property of Dave Mark */
- /* and may not be copied, distributed, etc. */
- /* */
- /****************************************************/
-
- #define kBaseResID 128
- #define kAboutALRTid 129
- #define kDialogResID 128
-
- #define kVisible true
- #define kMoveToBack NULL
- #define kMoveToFront (WindowPtr)-1L
- #define kNoGoAway false
- #define kSleep 60L
-
- #define kOn 1
- #define kOff 0
-
- #define iAfghan 1
- #define iElephant 2
- #define iSquirrel 3
-
- #define kLeftMargin 5
- #define kTopMargin 40
-
- #define kFirstRadio 1
- #define kLastRadio 3
-
- #define mApple kBaseResID
- #define iAbout 1
-
- #define mFile kBaseResID+1
- #define iSettings 1
- #define iQuit 3
-
-
- /*************/
- /* Globals */
- /*************/
-
- Boolean gDone;
- short gCurrentPICT = kBaseResID;
- DialogPtr gSettingsDLOG = NULL;
- WindowPtr gFredWindow = NULL;
-
-
- /***************/
- /* Functions */
- /***************/
-
- void ToolBoxInit( void );
- PicHandle LoadPICT( short picID );
- void CreateWindow( void );
- void MenuBarInit( void );
- void EventLoop( void );
- void DoEvent( EventRecord *eventPtr );
- void DoDialogEvent( EventRecord *eventPtr );
- void HandleMouseDown( EventRecord *eventPtr );
- void HandleMenuChoice( long menuChoice );
- void HandleAppleChoice( short item );
- void HandleFileChoice( short item );
- void DoUpdate( EventRecord *eventPtr );
- void CreateDialog( void );
- void FlipControl( ControlHandle control );
- void SwitchPICT( void );
-
-
- /******************************** main *********/
-
- void main( void )
- {
- ToolBoxInit();
- MenuBarInit();
-
- CreateWindow();
-
- EventLoop();
- }
-
-
- /*********************************** ToolBoxInit */
-
- void ToolBoxInit( void )
- {
- InitGraf( &thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( NULL );
- InitCursor();
- }
-
-
- /******************************** LoadPICT *********/
-
- PicHandle LoadPICT( short picID )
- {
- PicHandle pic;
-
- pic = GetPicture( picID );
-
- if ( pic == NULL )
- {
- SysBeep( 10 ); /* Couldn't load the PICT resource!!! */
- ExitToShell();
- }
- return( pic );
- }
-
-
- /******************************** CreateWindow *********/
-
- void CreateWindow( void )
- {
- PicHandle pic;
- Rect r;
-
- pic = LoadPICT( gCurrentPICT );
-
- r = (**pic).picFrame;
-
- OffsetRect( &r, kLeftMargin - r.left,
- kTopMargin - r.top );
-
- gFredWindow = NewWindow( NULL, &r, "\pMy Pet Fred", kVisible,
- noGrowDocProc, kMoveToBack, kNoGoAway, 0L );
-
- if ( gFredWindow == NULL )
- {
- SysBeep( 10 ); /* Couldn't load the WIND resource!!! */
- ExitToShell();
- }
-
- ShowWindow( gFredWindow );
- SetPort( gFredWindow );
- }
-
-
- /****************** MenuBarInit ***********************/
-
- void MenuBarInit( void )
- {
- Handle menuBar;
- MenuHandle menu;
-
- menuBar = GetNewMBar( kBaseResID );
- SetMenuBar( menuBar );
-
- menu = GetMHandle( mApple );
- AddResMenu( menu, 'DRVR' );
-
- DrawMenuBar();
- }
-
-
- /******************************** EventLoop *********/
-
- void EventLoop( void )
- {
- EventRecord event;
-
- gDone = false;
- while ( gDone == false )
- {
- if ( WaitNextEvent( everyEvent, &event, kSleep, NULL ) )
- DoEvent( &event );
- }
- }
-
-
- /************************************* DoEvent */
-
- void DoEvent( EventRecord *eventPtr )
- {
- char theChar;
-
- if ( IsDialogEvent( eventPtr ) )
- {
- DoDialogEvent( eventPtr );
- }
- else
- {
- switch ( eventPtr->what )
- {
- case mouseDown:
- HandleMouseDown( eventPtr );
- break;
- case keyDown:
- case autoKey:
- theChar = eventPtr->message & charCodeMask;
- if ( (eventPtr->modifiers & cmdKey) != 0 )
- HandleMenuChoice( MenuKey( theChar ) );
- break;
- case updateEvt:
- DoUpdate( eventPtr );
- break;
- }
- }
- }
-
-
- /************************************* DoDialogEvent */
-
- void DoDialogEvent( EventRecord *eventPtr )
- {
- short itemHit;
- short itemType;
- Handle itemHandle;
- Rect itemRect;
- short curRadioButton, i;
- char theChar;
- Boolean becomingActive;
- MenuHandle menu;
- DialogPtr dialog;
-
- menu = GetMHandle( mFile );
-
- switch ( eventPtr->what )
- {
- case keyDown:
- case autoKey:
- theChar = eventPtr->message & charCodeMask;
- if ( (eventPtr->modifiers & cmdKey) != 0 )
- HandleMenuChoice( MenuKey( theChar ) );
- break;
- case activateEvt:
- becomingActive = ( (eventPtr->modifiers & activeFlag) == activeFlag );
-
- if ( becomingActive )
- {
- for ( i=kFirstRadio; i<=kLastRadio; i++ )
- {
- GetDItem( gSettingsDLOG, i, &itemType, &itemHandle, &itemRect );
- HiliteControl( (ControlHandle)itemHandle, 0 );
- }
- DisableItem( menu, iSettings );
- }
- else
- {
- for ( i=kFirstRadio; i<=kLastRadio; i++ )
- {
- GetDItem( gSettingsDLOG, i, &itemType, &itemHandle, &itemRect );
- HiliteControl( (ControlHandle)itemHandle, 255 );
- }
- EnableItem( menu, iSettings );
- }
- break;
- }
-
- if ( DialogSelect( eventPtr, &dialog, &itemHit ) )
- {
- switch ( itemHit )
- {
- case iAfghan:
- case iElephant:
- case iSquirrel:
- curRadioButton = gCurrentPICT - kBaseResID + kFirstRadio;
-
- if ( curRadioButton != itemHit )
- {
- GetDItem( dialog, curRadioButton, &itemType,
- &itemHandle, &itemRect );
- FlipControl( (ControlHandle)itemHandle );
-
- GetDItem( dialog, itemHit, &itemType,
- &itemHandle, &itemRect );
- FlipControl( (ControlHandle)itemHandle );
-
- curRadioButton = itemHit;
-
- if ( gCurrentPICT != curRadioButton +
- kBaseResID - kFirstRadio )
- {
- gCurrentPICT = curRadioButton +
- kBaseResID - kFirstRadio;
- SwitchPICT();
- }
- }
- break;
- }
- }
- }
-
-
- /************************************* HandleMouseDown */
-
- void HandleMouseDown( EventRecord *eventPtr )
- {
- WindowPtr window;
- short thePart;
- long menuChoice;
- GrafPtr oldPort;
- long windSize;
- Rect growRect;
- MenuHandle menu;
-
- thePart = FindWindow( eventPtr->where, &window );
-
- switch ( thePart )
- {
- case inMenuBar:
- menuChoice = MenuSelect( eventPtr->where );
- HandleMenuChoice( menuChoice );
- break;
- case inSysWindow :
- SystemClick( eventPtr, window );
- break;
- case inContent:
- SelectWindow( window );
- break;
- case inDrag :
- DragWindow( window, eventPtr->where, &screenBits.bounds );
- break;
- case inGoAway:
- if ( TrackGoAway( window, eventPtr->where ) )
- if ( window == gSettingsDLOG )
- {
- HideWindow( window );
- menu = GetMHandle( mFile );
- EnableItem( menu, iSettings );
- }
- break;
- }
- }
-
-
- /****************** HandleMenuChoice ***********************/
-
- void HandleMenuChoice( long menuChoice )
- {
- short menu;
- short item;
-
- if ( menuChoice != 0 )
- {
- menu = HiWord( menuChoice );
- item = LoWord( menuChoice );
-
- switch ( menu )
- {
- case mApple:
- HandleAppleChoice( item );
- break;
- case mFile:
- HandleFileChoice( item );
- break;
- }
- HiliteMenu( 0 );
- }
- }
-
-
- /****************** HandleAppleChoice ***********************/
-
- void HandleAppleChoice( short item )
- {
- MenuHandle appleMenu;
- Str255 accName;
- short accNumber;
-
- switch ( item )
- {
- case iAbout:
- NoteAlert( kAboutALRTid, NULL );
- break;
- default:
- appleMenu = GetMHandle( mApple );
- GetItem( appleMenu, item, accName );
- accNumber = OpenDeskAcc( accName );
- break;
- }
- }
-
-
- /****************** HandleFileChoice ***********************/
-
- void HandleFileChoice( short item )
- {
- switch ( item )
- {
- case iSettings:
- if ( gSettingsDLOG == NULL )
- CreateDialog();
- else
- {
- ShowWindow( gSettingsDLOG );
- SelectWindow( gSettingsDLOG );
- }
- break;
- case iQuit:
- gDone = true;
- break;
- }
- }
-
-
- /************************************* DoUpdate */
-
- void DoUpdate( EventRecord *eventPtr )
- {
- PicHandle pic;
- WindowPtr window;
- Rect r;
-
- window = (WindowPtr)eventPtr->message;
-
- pic = LoadPICT( gCurrentPICT );
-
- SetPort( window );
-
- BeginUpdate( window );
-
- r = window->portRect;
- DrawPicture( pic, &r );
-
- EndUpdate( window );
- }
-
-
- /************************************* CreateDialog */
-
- void CreateDialog( void )
- {
- short itemType;
- Handle itemHandle;
- Rect itemRect;
- short curRadioButton;
-
- gSettingsDLOG = GetNewDialog( kDialogResID, NULL, kMoveToFront );
-
- if ( gSettingsDLOG == NULL )
- {
- SysBeep( 10 ); /* Couldn't load the DLOG resource!!! */
- ExitToShell();
- }
-
- ShowWindow( gSettingsDLOG );
- SetPort( gSettingsDLOG );
-
- curRadioButton = gCurrentPICT - kBaseResID + kFirstRadio;
- GetDItem( gSettingsDLOG, curRadioButton, &itemType, &itemHandle, &itemRect );
- SetCtlValue( (ControlHandle)itemHandle, kOn );
- }
-
-
- /************************************* FlipControl */
-
- void FlipControl( ControlHandle control )
- {
- SetCtlValue( control, ! GetCtlValue( control ) );
- }
-
-
- /************************************* SwitchPICT */
-
- void SwitchPICT( void )
- {
- DisposeWindow( gFredWindow );
-
- CreateWindow();
- }